<?php
session_start();

define('IGNORE_MOBILE', true);

require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

$unique_id = $_GET['vid'];

if ( ! empty($unique_id) && strlen($unique_id) < 10) 
{
	$video = request_video($unique_id);
	
	if ( ! $logged_in && $video['restricted'] == '1')
	{
		$video_is_restricted = true;
	}
}


// pre-roll static ads
$display_preroll_ad = false;
if ($config['total_preroll_ads'] > 0)
{
	if (isset($_COOKIE[COOKIE_PREROLLAD]) && strlen($_COOKIE[COOKIE_PREROLLAD]) > 0)
	{
		$display_preroll_ad = false;
	}
	else
	{
		$preroll_ad_data = get_preroll_ad();
	
		if (is_array($preroll_ad_data))
		{
			$display_preroll_ad = true;
			$preroll_ad_data['timeleft_start'] = ($preroll_ad_data['duration'] > 60) ? sec2hms($preroll_ad_data['duration']) : $preroll_ad_data['duration'];
		}
		else
		{
			$smarty->assign('display_preroll_ad', false);
		}
	}
}

//
// ---- output ----
//  
?>
<!DOCTYPE html>
<!--[if IE 7 | IE 8]>
<html class="ie" dir="ltr" lang="en">
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html dir="ltr" lang="en">
<!--<![endif]-->
<head>
<meta charset="UTF-8" />
<title><?php echo $video['video_title'];?></title>
<link rel="canonical" href="<?php echo makevideolink($video['uniq_id'], $video['video_title']);?>">
<style type="text/css">
html{overflow:hidden}
body{margin:0;padding:0;border:0;font-size:100%;font:12px Arial,sans-serif;background-color:#000;color:#fff;height:100%;width:100%;overflow:hidden;position:absolute;-webkit-tap-highlight-color:rgba(0,0,0,0)}
a{color:#fff}
p{padding:5px 10px}
object,embed,iframe{margin:0;padding:0;border:0;font-size:100%;background:transparent}
.full-frame{width:100%;height:100%}
h1,h2,h3,h4,h5{text-align:center;color:#fff}
#preroll_placeholder{position:relative;display:block;width:100%;text-align:center}
#preroll_placeholder embed,iframe{max-width:99%;}
.preroll_countdown{padding:3px 0}
.embed_logo img{max-width:95%}
.restricted-video{display:block;height:100%;background:url('<?php echo show_thumb($video['uniq_id'], 1, $video); ?>') no-repeat center center}
.btn-success{margin:0 auto;display:block;width:130px;font-size:11px;font-weight:bold;text-align:center;text-decoration:none;padding:5px 10px;color:#fff;text-shadow:0 -1px 0 rgba(0,0,0,0.25);background-color:#77a201;border-width:2px;border-style:solid;border-color:#688e00 #688e00 #8eaf33;border-color:rgba(0,0,0,0.1) rgba(0,0,0,0.1) rgba(0,0,0,0.25);-moz-border-radius:4px;-webkit-border-radius:4px;border-radius:4px;box-shadow:0 1px 3px #000}
.btn-success:hover,.btn-success:active,.btn-success.active,.btn-success.disabled,.btn-success[disabled]{color:#fff;background-color:#8eaf33;box-shadow:none}
.btn-success:active,.btn-success.active{background-color:#3C0}
@-o-viewport{width:device-width}
@-moz-viewport{width:device-width}
@-ms-viewport{width:device-width}
@-webkit-viewport{width:device-width}
@viewport{width:device-width}
</style>
<?php if ($display_preroll_ad) : ?>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
	<script src="<?php echo _URL; ?>/js/jquery.timer.min.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$('#video_player_container').hide();
		});
	</script>
<?php endif; ?>
</head>
<body>
	
	<?php if ( ! $video) : // video not found ?>
		<h1><?php echo $lang['sorry'];?></h1>
        <h3><?php echo $lang['video_not_found'];?></h3>
		<p>
			<?php if ($config['custom_logo_url'] != '') : ?> 
				<div class="embed_logo" align="center"><a href="<?php echo _URL;?>" target="_blank"><img src="<?php echo $config['custom_logo_url'];?>" alt="<?php echo _SITENAME;?>" title="<?php echo _SITENAME;?>" border="0"></a></div>
			<?php else : ?>
				<h3><a href="<?php echo _URL;?>" target="_blank"><?php echo _SITENAME;?></a></h3>
			<?php endif; ?>
		</p>
</body>
</html>
	<?php
			exit(); 
		endif;
	?>
	
	<?php if ($video_is_restricted) : ?>
	<div class="restricted-video">
		<h2><?php echo $video['video_title'];?></h2>
        <h3><?php echo $lang['restricted_sorry'];?></h3>
		<p>
			<a href="<?php echo makevideolink($video['uniq_id'], $video['video_title']);?>" target="_blank" class="btn-success"><?php echo $lang['proceed'];?></a>
		</p>
	</div>
</body>
</html>
	<?php
			exit(); 
		endif;
	?>
	<?php if ($display_preroll_ad) : ?>
		<div id="preroll_placeholder">
			<div class="preroll_countdown">
				<?php echo $lang['preroll_ads_timeleft']; ?> <span class="preroll_timeleft"><?php echo $preroll_ad_data['timeleft_start'];?></span>
			</div>
			<?php echo $preroll_ad_data['code'];?>
		</div>
	<?php 
		endif;
	?>
		<span id="video_player_container">
			<?php 
			$embed_code = generate_embed_code($video['uniq_id'], $video, false, 'embed');
			$embed_code = str_replace('width="'. _PLAYER_W_EMBED .'"', 'width="100%"', $embed_code);
			$embed_code = str_replace('height="'. _PLAYER_H_EMBED .'"', 'height="100%"', $embed_code);
			if ($config['video_player'] == 'jwplayer6')
			{
				$embed_code = str_replace("width: '". _PLAYER_W_EMBED ."'", "width: '100%'", $embed_code);
				$embed_code = str_replace("height: '". _PLAYER_H_EMBED ."'", "height: '100%'", $embed_code);
			}
			echo $embed_code;
			?>
		</span>
	<!-- Footer -->
	
	<?php if ($display_preroll_ad) : ?>
	<script type="text/javascript">
	
	function timer_pad(number, length) {
		var str = '' + number;
		while (str.length < length) {str = '0' + str;}
		return str;
	}
	
	var preroll_timer;
	var preroll_player_called = false; // backup 
	
	$(document).ready(function(){
	
		var preroll_timer_current = <?php echo $preroll_ad_data['duration'];?> * 1000;
		
		preroll_timer = $.timer(function(){
		
			var seconds = parseInt(preroll_timer_current / 1000);
			var hours = parseInt(seconds / 3600);
			var minutes = parseInt((seconds / 60) % 60);
			var seconds = parseInt(seconds % 60);
			
			var output = "00";
			if (hours > 0) {
				output = timer_pad(hours, 2) +":"+ timer_pad(minutes, 2) +":"+ timer_pad(seconds, 2);
			} else if (minutes > 0) { 
				output = timer_pad(minutes, 2) +":"+ timer_pad(seconds, 2);
			} else {
				output = timer_pad(seconds, 1);
			}
			
			$('.preroll_timeleft').html(output);
			
			if (preroll_timer_current == 0 && preroll_player_called == false) {
								
				//$('#preroll_placeholder').replaceWith(video_embed_code);
				$('#preroll_placeholder').hide();
				$('#video_player_container').show();

				preroll_player_called = true;
				preroll_timer.stop();
			} else {
				preroll_timer_current -= 1000;
				if(preroll_timer_current < 0) {
					preroll_timer_current = 0;
				}
			}
		}, 1000, true);
	});
	</script>
	<?php endif; ?>
</body>
</html>